import heapq
import collections


class Solution:
    def maxSlidingWindow(self, nums: list, k: int) -> list:
        array = []
        counter = collections.Counter()
        for i in range(k):
            counter[nums[i]] += 1
            if counter[nums[i]] == 1:
                heapq.heappush(array, -nums[i])
        left = 0
        right = k - 1
        ans = []
        while right < len(nums):
            top = -array[0]
            while counter[top] == 0:
                heapq.heappop(array)
                top = -array[0]
            ans.append(top)
            counter[nums[left]] -= 1
            left += 1
            right += 1
            if right >= len(nums):
                continue
            counter[nums[right]] += 1
            if counter[nums[right]] == 1:
                heapq.heappush(array, -nums[right])

        return ans
